package com.wangjie.exception;

import com.wangjie.response.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author jieshao
 * @date 2021/11/26 11:24
 */
@ControllerAdvice
public class ExceptionCatch {
    private static final Logger logger = LoggerFactory.getLogger(ExceptionCatch.class);

    // 捕获业务异常
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public AjaxResult businessException(BusinessException e) {
        logger.error("catch exception: {}", e.getMessage());
        return AjaxResult.error(e.getMessage());
    }

    // 捕获系统异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public AjaxResult exception(Exception e) {
        logger.error("catch exception: {}", e.getMessage());
        return AjaxResult.error("服务端返回未知错误");
    }
}